﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Security.Cryptography.X509Certificates;
using SSLVerifier.API.MainLogic;

namespace SSLVerifier.API.ModelObjects {
	public sealed class ChainElement : INotifyPropertyChanged {
		private String name;
		private X509Certificate2 cert;
		ChainElement parent;

		public ChainElement() {
			Child = new ObservableCollection<ChainElement>();
			PropagatedErrors = NativeErrors = new List<X509ChainStatusFlags2>();
		}

		public String Name {
			get { return name; }
			set {
				name = value;
				OnPropertyChanged("Name");
			}
		}

		public X509Certificate2 Certificate {
			get { return cert; }
			set {
				cert = value;
				OnPropertyChanged("Certificate");
			}
		}
		public ChainElement Parent {
			get { return parent; }
			set {
				parent = value;
				OnPropertyChanged("Parent");
			}
		}
		public String NativeErrorString {
			get {
				StringBuilderWrapper SB = new StringBuilderWrapper();
				foreach (X509ChainStatusFlags2 status in NativeErrors) {
					SB.Add(status + Environment.NewLine);
				}
				return SB.Text;
			}
		}
		public String PropagatedErrorString {
			get {
				StringBuilderWrapper SB = new StringBuilderWrapper();
				foreach (X509ChainStatusFlags2 status in PropagatedErrors) {
					SB.Add(status + Environment.NewLine);
				}
				return SB.Text;
			}
		}
		public ObservableCollection<ChainElement> Child { get; set; }
		public Boolean IsRoot { get; set; }
		public Boolean HasErrors { get; set; }
		public Boolean HasWarnings { get; set; }
		public List<X509ChainStatusFlags2> NativeErrors { get; set; }
		public List<X509ChainStatusFlags2> PropagatedErrors { get; set; }

		void OnPropertyChanged(String PropertyName) {
			PropertyChangedEventHandler handler = PropertyChanged;
			if (handler != null) {
				handler(this, new PropertyChangedEventArgs(PropertyName));
			}
		}
		public event PropertyChangedEventHandler PropertyChanged;
	}
}
